﻿using MediatR;
using Swork03_28.Api.Application.Commands.TraceableCommand;
using Swork03_28.Services.Interface;

namespace Swork03_28.Api.Application.CommandHandlers.TraceableHandler
{
    public class TraceableCreateTestImgCommandHandler : IRequestHandler<TraceableCreateTestImgCommand, int>
    {
        private readonly ITraceableService traceableService;

        public TraceableCreateTestImgCommandHandler(ITraceableService traceableService)
        {
            this.traceableService = traceableService;
        }

        public async Task<int> Handle(TraceableCreateTestImgCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            var info = await traceableService.GetAsync(request.Id);
            var res = false;
            if (info.TestImgs != null) { res = info.TestImgs.Split(",").Contains(request.Img); }
            if (res)
            {
                return count;
            }
            info.TestImgs = (info.TestImgs + "," + request.Img).TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());
            count += await traceableService.UpdateAsync(info);
            return count;
        }
    }
}
